package com.dongdongshop.controller;

import com.dongdongshop.domain.AjaxResult;
import com.dongdongshop.domain.PageResult;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.BrandService;
import com.dongdongshop.service.SpecificationService;
import com.dongdongshop.service.TypeTemplateService;
import em.ResuleEnum;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("typetemplate")
public class TypeTemplateController {

    @DubboReference
    private TypeTemplateService typeTemplateService;
    @DubboReference
    private BrandService brandService;
    @DubboReference
    private SpecificationService specificationService;

    @RequestMapping("typetemplateShow")
    public String typetemplateShow(Model model){
        List<TbBrand> brandList = brandService.brandList1();
        model.addAttribute("brandList",brandList);
        List<TbSpecification> tbSpecificationList  = specificationService.getInfo1();
        model.addAttribute("tbSpecificationList",tbSpecificationList);
        return "admin/type_template";
    }

    @RequestMapping("typetemplateList")
    @ResponseBody
    public AjaxResult typetemplateList(@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "3")Integer pageSize,String name){
        PageResult<TbTypeTemplate> list = typeTemplateService.typetemplateList(pageNum,pageSize,name);
        return AjaxResult.R(ResuleEnum.SUCCESS).setData(list);
    }

    @RequestMapping("toAdd")
    @ResponseBody
    public AjaxResult toAdd(TbTypeTemplate typeTemplate,String brandListJson,String specListJson,String kuozhanListJson){
        typeTemplateService.toAdd(typeTemplate,brandListJson,specListJson,kuozhanListJson);
        return AjaxResult.ok();
    }

    @RequestMapping("selectById")
    @ResponseBody
    public AjaxResult selectById(Long id){
            TbTypeTemplate typeTemplate = typeTemplateService.selectById(id);
            return AjaxResult.ok().setData(typeTemplate);
    }

    @RequestMapping("updateInfo")
    @ResponseBody
    public AjaxResult updateInfo(TbTypeTemplate typeTemplate,String brandListJson,String specListJson,String kuozhanListJson){
        typeTemplateService.updateInfo(typeTemplate,brandListJson,specListJson,kuozhanListJson);
        return AjaxResult.ok();
    }

    @RequestMapping("deleteInfo")
    @ResponseBody
    public AjaxResult deleteInfo(@RequestParam("ids[]")Long[] ids){
        typeTemplateService.deleteInfo(ids);
        return AjaxResult.ok();
    }
}
